package com.cloudcc.mobile.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudcc.cloudframe.CApplication;
import com.cloudcc.cloudframe.model.UserModel;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.entity.UserBasicInfoForUpdate;
import com.cloudcc.mobile.manager.UserManager;
import com.cloudcc.mobile.util.UrlTools;
import com.cloudcc.mobile.util.UserUtils;
import com.cloudcc.mobile.view.base.BaseActivity;
import com.litesuits.android.log.Log;

public class NeweditorActivity extends BaseActivity {
	private TextView tv1, tv2;
	private ImageView imageView, back;
	private TextView tv_name, tv_username, tv_jiaose, tv_jiandang, tv_phone,
			tv_shouji;
	private UserBasicInfoForUpdate mUserBasicInfo;
	private Button button;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		super.onCreate(savedInstanceState);
		setContentView(R.layout.newusermessage);
		tv1 = (TextView) findViewById(R.id.user_name);
		tv2 = (TextView) findViewById(R.id.user_company);
		imageView = (ImageView) findViewById(R.id.user_follow);
		tv_name = (TextView) findViewById(R.id.tv_user_name);
		tv_username = (TextView) findViewById(R.id.tv_username);
		tv_jiaose = (TextView) findViewById(R.id.tv_jiaose);
		tv_jiandang = (TextView) findViewById(R.id.tv_jiandang);
		tv_phone = (TextView) findViewById(R.id.tv_phone);
		tv_shouji = (TextView) findViewById(R.id.tv_shouji);
		button = (Button) findViewById(R.id.btn_user_message);
		back = (ImageView) findViewById(R.id.backuser);
		initdata();
	}

	@Override
	public int getLayoutId() {
		return R.layout.newusermessage;
	}

	private void initdata() {
		// TODO Auto-generated method stub
		mUserBasicInfo = (UserBasicInfoForUpdate) getIntent()
				.getSerializableExtra("chatuser");

		//最上面的标题name
		tv1.setText(mUserBasicInfo.getName());
		tv2.setText(CApplication.loginname);
//		tv2.setText(mUserBasicInfo.getEmail());
		tv_name.setText(mUserBasicInfo.getName());
		tv_phone.setText(mUserBasicInfo.getPhone());
		tv_shouji.setText(mUserBasicInfo.getMobile());
		
		tv_jiandang.setText(CApplication.jiandang);
		tv_jiaose.setText(CApplication.juese);
		tv_username.setText(mUserBasicInfo.getEmail());
		String userId = UserManager.getManager().getUser().userId;
		String url = UrlTools.getTopImage(userId);
		UserUtils.setLogoRound_z(url, imageView,
				UserUtils.getBackGroudColorByUserIdHash(userId, this),
				UserUtils.getNickRowName(mUserBasicInfo.getName()), this);
		// imageView.setImageBitmap(MainUIActivity.imgphoto);
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				Toast.makeText(NeweditorActivity.this, "点击编辑", 1).show();
				UserBasicInfoForUpdate userBasicInfoForUpdate = new UserBasicInfoForUpdate();
				Intent intent = new Intent(NeweditorActivity.this,
						UpdateInfoActivity.class);
				// Intent intent = new Intent(mContext,
				// NeweditorActivity.class);
				userBasicInfoForUpdate.setEmail(mUserBasicInfo.getEmail());
				userBasicInfoForUpdate.setMobile(mUserBasicInfo.getMobile());
				userBasicInfoForUpdate.setPhone(mUserBasicInfo.getPhone());
				userBasicInfoForUpdate.setTitle(mUserBasicInfo.getTitle());
				userBasicInfoForUpdate.setAddress(mUserBasicInfo.getAddress());
				userBasicInfoForUpdate.setName(mUserBasicInfo.getName());
				intent.putExtra("chatuser", userBasicInfoForUpdate);//
				startActivityForResult(intent, 701);
			}
		});
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setResult(RESULT_OK);
				finish();
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 701 && resultCode == Activity.RESULT_OK) {
			Log.d("activityresultcode", 1 + " " + resultCode + requestCode);

			UserBasicInfoForUpdate basic = (UserBasicInfoForUpdate) data
					.getSerializableExtra("user.new");
			mUserBasicInfo.setEmail(basic.getEmail());
			mUserBasicInfo.setMobile(basic.getMobile());
			mUserBasicInfo.setPhone(basic.getPhone());
			mUserBasicInfo.setTitle(basic.getTitle());
			mUserBasicInfo.setAddress(basic.getAddress());
			// mUserBasicInfo.setAboutme(basic.getAbout());
			UserModel user = UserManager.getManager().getUser();

//			user.email=mUserBasicInfo.email;
//			user.mobilePhone=mUserBasicInfo.getMobile();

			user.email = user.email;
			user.mobilePhone = user.mobilePhone;

			UserManager.getManager().saveUser(user);
			initdata();
		}
	}

}
